﻿using System;
using System.Data;
using System.Windows.Forms;
using template_dll;
using System.Data.SqlClient;

namespace ServerClient
{
    public partial class LogsForm : Form
    {
        public LogsForm()
        {
            InitializeComponent();
        }

        private SqlConnection con = new SqlConnection(@"Server=sdata2;Database=ExchangeFilesDB;Integrated Security=True");

        private void LogsForm_Load(object sender, EventArgs e)
        {
            dateTimePicker1.Value = DateTime.Now;
            dateTimePicker2.Value = DateTime.Now.AddHours(-1);
            dateTimePicker3.Value = DateTime.Now;
            dateTimePicker4.Value = DateTime.Now;
            LogsSel();
        }

        public void LogsSel()
        {

            try
            {
                SPparams[] sp = new SPparams[2];
                sp[0] = new SPparams() { Name = "@datestart", Value = dateTimePicker1.Value.Date.ToString("yyyyMMdd") + " " + dateTimePicker2.Value.ToString("HH:mm:ss") };
                sp[1] = new SPparams() { Name = "@dateend", Value = dateTimePicker4.Value.Date.ToString("yyyyMMdd") + " " + dateTimePicker3.Value.ToString("HH:mm:ss") };
                if (textBox1.Text != "")
                {
                    Array.Resize(ref sp, sp.Length + 1);
                    sp[sp.Length - 1] = new SPparams() { Name = "@clientname", Value = textBox1.Text };
                }
                if (textBox2.Text != "")
                {
                    Array.Resize(ref sp, sp.Length + 1);
                    sp[sp.Length - 1] = new SPparams() { Name = "@pathfile", Value = textBox2.Text };
                }
                if (textBox4.Text != "")
                {
                    Array.Resize(ref sp, sp.Length + 1);
                    sp[sp.Length - 1] = new SPparams() { Name = "@error", Value = textBox4.Text };
                }
                if (textBox5.Text != "")
                {
                    Array.Resize(ref sp, sp.Length + 1);
                    sp[sp.Length - 1] = new SPparams() { Name = "@logmessage", Value = textBox5.Text };
                }
                dataGridView1.DataSource = null;
                dataGridView1.DataSource = new SqlStorProc().SqlProcExecWithQueryOneTable(con, "LogsSel", 30, sp).DefaultView;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (con.State != ConnectionState.Closed) con.Close();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            LogsSel();
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) LogsSel();
        }


    }
}
